package cn.jari.erp.inventory.entities.primary;
import cn.jari.erp.inventory.entities.primary.InventoryCategory;
import cn.jari.erp.inventory.entities.primary.InventoryItem;
import com.csicit.ace.jpaleaf.LeafIdGenerator;
import com.csicit.ace.jpaleaf.entity.SourceIdentifiedAndVersionedEntity;
import com.csicit.ace.jpautils.recycle.RecyclableEntity;
import com.csicit.ace.jpautils.sortpathtree.SortPathTreeEntity;
import com.csicit.ace.model.PropertyAccess;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.annotations.GenericGenerator;
import java.util.Objects;
import org.hibernate.Hibernate;
import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;

/*
 * 警告：代码由鲲舟平台生成，请勿手动修改。
 * Warn: This class file is generated by JARI-ACE platform, please do not modify manually。
 */

/**
 * 存货类别的实体基类
 * 
 *
 * @author JARI-ACE
 */
@Getter
@Setter
@MappedSuperclass
public abstract class AbstractInventoryCategory implements SortPathTreeEntity<InventoryCategory, String>, SourceIdentifiedAndVersionedEntity<String>, RecyclableEntity {
    /**
     * 主键
     */
    @Column(name = "id")
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = LeafIdGenerator.GENERATOR_NAME)
    @GenericGenerator(name = LeafIdGenerator.GENERATOR_NAME, strategy = LeafIdGenerator.CLASS_NAME)
    private String id = null;
    /**
     * 是否已回收
     */
    @Column(name = "recycled")
    private Boolean recycled = null;
    /**
     * 回收键
     */
    @Column(name = "recycle_index")
    private String recycleIndex = null;
    /**
     * 数据版本
     */
    @Column(name = "data_version")
    @Version
    private Long dataVersion = null;
    /**
     * 密级
     */
    @Column(name = "classification_level")
    private Short classificationLevel = null;
    /**
     * 创建用户ID
     */
    @Column(name = "created_by_user")
    private String createdByUser = null;
    /**
     * 创建用户姓名
     */
    @Column(name = "created_by_user_name")
    private String createdByUserName = null;
    /**
     * 创建部门ID
     */
    @Column(name = "created_by_dept")
    private String createdByDept = null;
    /**
     * 创建部门名称
     */
    @Column(name = "created_by_dept_name")
    private String createdByDeptName = null;
    /**
     * 创建用户域名
     */
    @Column(name = "created_by_realm_name")
    private String createdByRealmName = null;
    /**
     * 创建时间
     */
    @Column(name = "created_time")
    private LocalDateTime createdTime = null;
    /**
     * 发布源站点ID
     */
    @Column(name = "release_source")
    private String releaseSource = null;
    /**
     * 源ID
     */
    @Column(name = "source_id")
    private String sourceId = null;
    /**
     * 是否有子节点
     */
    @Column(name = "has_children")
    private Boolean hasChildren = null;
    /**
     * 父节点
     */
    @ManyToOne
    @JoinColumn(name = "parent_id")
    private InventoryCategory parent = null;
    /**
     * 子节点集合
     */
    @OneToMany(mappedBy = "parent", cascade = {})
    private Set<InventoryCategory> children = new HashSet<>();
    /**
     * 节点路径
     */
    @Column(name = "sort_path")
    private String sortPath = null;
    /**
     * 同级序号
     */
    @Column(name = "sort_index")
    private Integer sortIndex = null;
    /**
     * 类别属性
     */
    @Column(name = "attributes")
    @ElementCollection
    @CollectionTable(name = "iv_inventory_category_attrs", joinColumns = @JoinColumn(name = "inventory_category_id"))
    @OrderColumn(name = "sort_index")
    @MapKeyColumn(name = "map_key")
    private Map<String,String> attributes = new LinkedHashMap<>();
    public Map<String,String> getAttributes() {
        return PropertyAccess.read(this.getClass(), "attributes", attributes);
    }
    /**
     * 类别名称
     */
    @Column(name = "name")
    private String name = null;
    /**
     * 存货档案
     */
    @OneToMany(mappedBy = "inventoryCategory", cascade = {})
    @OrderBy("id ASC")
    private List<InventoryItem> inventoryItem = new ArrayList<>();
    public List<InventoryItem> getInventoryItem() {
        return PropertyAccess.read(this.getClass(), "inventoryItem", inventoryItem);
    }

    @Override
    public void setParent(InventoryCategory parent) {
        if (Objects.equals(this.parent, parent)) {
            return;
        }
        if (this.parent != null && Hibernate.isInitialized(this.parent.getChildren())) {
            this.parent.getChildren().remove((InventoryCategory) this);
        }
        originalParent = this.parent;
        this.parent = parent;
        if (parent != null && Hibernate.isInitialized(this.parent.getChildren())) {
            parent.getChildren().add((InventoryCategory) this);
        }
    }

    @Override
    public void setSortIndex(Integer sortIndex) {
        if (!Objects.equals(sortIndex, this.sortIndex)) {
            this.originalSortIndex = this.sortIndex;
            this.sortIndex = sortIndex;
        }
    }

    /**
    * 原父节点
    */
    @Setter
    @Transient
    private InventoryCategory originalParent;

    /**
    * 原序号
    */
    @Transient
    private Integer originalSortIndex;

    @Override
    public InventoryCategory getOriginalParent() {
        return originalParent;
    }

    @Override
    public Integer getOriginalSortIndex() {
        return originalSortIndex;
    }

    /**
     * 同步保留的历史状态数据
     */
    @Override
    public void syncState() {
        this.originalParent = this.parent;
        this.originalSortIndex = this.sortIndex;
    }
}